//
//  TTRecyclingDetailViewController.m
//  TaoTuo
//
//  Created by zhaoshijie on 2020/9/4.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingDetailViewController.h"
#import "TTRecyclingDetailHeaderView.h"
#import "AppDelegate.h"
#import "TTImageTitleView.h"
#import "TTShoutOutPriceViewCell.h"
#import "TTDetailImageViewCell.h"
#import "TTUpDownButton.h"
#import "TTRecyclingDetailModel.h"
#import "YBImageBrowser.h"
#import "TTDiamondPopShareView.h"
#import "TTRecyclingHomeRightNavbarViewManager.h"
#import "TTPriceChangeView.h"
#import "TTRecyclingCommitOrderViewController.h"
#import "TTRecyclingShareView.h"
#import "TTRecyclingAskPriceView.h"
#import <TYAlertController.h>

static NSString *cellType = @"cell_type";
static NSString *cellDatas = @"cell_datas";

typedef enum : NSUInteger {
    CellTypeWithPriceInfo,
    CellTypeWithTitleHeader,
    CellTypeWithContentInfoHeader,
    CellTypeWithImage,
} CellType;

static NSString *priceInfoCellID = @"price_info_cell_id";
static NSString *imagesCellID = @"images_cell_id";

@interface TTRecyclingDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UIView *navBarView;
@property (nonatomic, strong) TTRecyclingDetailHeaderView *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy) NSString *goodsID;

@property (nonatomic, strong) TTRecyclingDetailModel *detailModel;
/// 货品描述sectionView高度
@property (nonatomic, assign) CGFloat descSectionH;
/// 底部按钮
@property (nonatomic, strong) UIButton *bottomBtn;

@property (nonatomic, assign) GoodsStatus status;

@property (nonatomic, strong) TTPriceChangeView *priceChangeView;
@property (nonatomic, strong) TTRecyclingShareView *shareView;
/// 要分享的图片
@property (nonatomic, strong) UIImage *shareImage;

/// 询价弹窗
@property (nonatomic, strong) TTRecyclingAskPriceView *askPriceView;

@end

@implementation TTRecyclingDetailViewController

- (instancetype)initWithGoodsID:(NSString *)goodsID
{
    self = [super init];
    if (self) {
        
        self.goodsID = goodsID;
        
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[TTRecyclingHomeRightNavbarViewManager sharedManager] hiddenRightBarView];
    [TTRecyclingHomeRightNavbarViewManager sharedManager].rightBarViewBlock = ^(NSInteger index) {
        NSLog(@"像打瞌睡");
    };
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.headerView.canRotation = NO;
    [self.headerView.subject sendNext:@(NO)];
    self.navigationController.navigationBar.alpha = 1;
    [[TTRecyclingHomeRightNavbarViewManager sharedManager] showRightBarView];
    [TTRecyclingHomeRightNavbarViewManager sharedManager].rightBarViewBlock = nil;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];

    [self.headerView.playerView pause];
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"商品详情";
    //设置为向上延展，不设置的话，当导航栏变为不透明的时候，页面会向下偏移
    self.edgesForExtendedLayout = UIRectEdgeTop;
    self.view.backgroundColor = [UIColor whiteColor];

    [self navBarView];

    [self requestDetailDatas];
    
    UIImage *image = [[UIImage imageNamed:@"detailshare"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:@selector(shareAction)];

}

- (void)requestDetailDatas
{
    if (self.goodsID.length == 0) {
        [self.view makeToast:@"暂无商品信息"];
        return;
    }
    
    NSDictionary *dic = @{
        @"param.informationGoodsId" : self.goodsID
    };
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager]requestMethod:GET urlString:recycling_detail_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        self.detailModel = [TTRecyclingDetailModel modelWithJSON:responseObject];
        
        [self setupBottomBtnTitle];
        
        TTRecyclingDetailPriceListModel *priceModel = self.detailModel.Offer_Price_List.firstObject;
        priceModel.isShowRanking = YES;
        
        self.descSectionH = [self.detailModel.Describe boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 30, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesDeviceMetrics attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size.height + 10;
            
        self.headerView.model = self.detailModel;
        
        NSMutableArray *images = [NSMutableArray array];
        for (NSString *imageUrlString in self.detailModel.Goods_PictureVideo) {
            TTDetailImageModel *imgModel = [[TTDetailImageModel alloc] init];
            imgModel.imageUrlStr = imageUrlString;
            imgModel.imageHeight = 0;
            [images addObject:imgModel];
        }
        
        self.detailModel.bottomImagesArray = images;
        
        if (self.detailModel.ShareImage.length > 0) {
            NSURL *url = [NSURL URLWithString:self.detailModel.ShareImage];
            [[YYWebImageManager sharedManager] requestImageWithURL:url options:YYWebImageOptionAllowBackgroundTask progress:nil transform:nil completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
                self.shareImage = image;
                NSLog(@"分享图片下载完成");
            }];
        }
        
        [self setupDatas];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
    
}

- (void)setupDatas
{
    [self.dataArray removeAllObjects];

    if (self.detailModel.SalesModel == 2) {
        [self.dataArray addObject:@{cellType : @(CellTypeWithPriceInfo), cellDatas : self.detailModel.Offer_Price_List ? : @[]}];
    }
    [self.dataArray addObject:@{cellType : @(CellTypeWithTitleHeader)}];
    [self.dataArray addObject:@{cellType : @(CellTypeWithContentInfoHeader)}];
    [self.dataArray addObject:@{cellType : @(CellTypeWithImage), cellDatas : self.detailModel.bottomImagesArray}];

    self.tableView.tableHeaderView = self.headerView;
    [self.tableView reloadData];
}

// 设置底部按钮标题
- (void)setupBottomBtnTitle
{
    NSString *title = @"";
    BOOL isOwner = [[TTLoginSingle sharedSingle].loginModel.RelationId isEqualToString:self.detailModel.RelationId];
    if (self.detailModel.SalesModel == 1) {
        // 询价模式
        if (self.detailModel.SalesState == 1) {
            title = @"立即询价";
            self.bottomBtn.backgroundColor = RGB(210, 79, 75);
        } else if (self.detailModel.SalesState == 2) {
            title = @"已出售";
            self.bottomBtn.backgroundColor = RGB(224, 224, 224);
            self.bottomBtn.enabled = NO;
        } else {
            title = @"已下架";
            self.bottomBtn.backgroundColor = RGB(224, 224, 224);
            self.bottomBtn.enabled = NO;
        }
        
    } else if (self.detailModel.SalesModel == 2) {
        // 竞价模式，由headerview中的block回调处理
//        title = isOwner ? @"自己的货品不能出价" : @"立即出价";
//        self.bottomBtn.backgroundColor = isOwner ? RGB(224, 224, 224) : RGB(210, 79, 75);
//        self.bottomBtn.enabled = !isOwner;
    } else if (self.detailModel.SalesModel == 3) {
        // 一口价
        if (self.detailModel.SalesState == 1) {
            title = isOwner ? @"自己的货品不能购买" : @"立即购买";
            self.bottomBtn.backgroundColor = isOwner ? RGB(224, 224, 224) : RGB(210, 79, 75);
            self.bottomBtn.enabled = !isOwner;
        } else if (self.detailModel.SalesState == 2) {
            title = @"已出售";
            self.bottomBtn.backgroundColor = RGB(224, 224, 224);
            self.bottomBtn.enabled = NO;
        } else {
            title = @"已下架";
            self.bottomBtn.backgroundColor = RGB(224, 224, 224);
            self.bottomBtn.enabled = NO;
        }
    }
    
    [self.bottomBtn setTitle:title forState:UIControlStateNormal];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    return [dic[cellDatas] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArray[indexPath.section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo) {
        TTShoutOutPriceViewCell *cell = [tableView dequeueReusableCellWithIdentifier:priceInfoCellID];
        [cell setModel:dic[cellDatas][indexPath.row] priceDifference:self.detailModel.Price_Difference];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UIColor *color = indexPath.row % 2 == 0 ? UIColorHex(#F9F9F9) : UIColor.whiteColor;
        cell.backgroundColor = color;
        return cell;
    } else if ([dic[cellType] intValue] == CellTypeWithImage) {
        TTDetailImageModel *imageModel = dic[cellDatas][indexPath.row];
        TTDetailImageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:imagesCellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        @weakify(self)
        [cell.pictureView sd_setImageWithURL:[NSURL URLWithString:imageModel.imageUrlStr] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            @strongify(self)
            if (image != nil) {
                CGSize imageSize = image.size;
                imageModel.imageHeight = imageSize.height * SCREEN_WIDTH / imageSize.width;
                NSIndexPath *ip = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];
                [self.tableView beginUpdates];
                [self.tableView reloadRowsAtIndexPaths:@[ip] withRowAnimation:UITableViewRowAnimationFade];
                [self.tableView endUpdates];
            }
        }];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArray[indexPath.section];
    if ([dic[cellType] intValue] == CellTypeWithImage) {
        @autoreleasepool {
            NSMutableArray *imgs = [NSMutableArray array];
            for (TTDetailImageModel *model in dic[cellDatas]) {
                if (model.imageUrlStr.length > 0) {
                    YBIBImageData *imageData = [YBIBImageData new];
                    imageData.imageURL = [NSURL URLWithString:model.imageUrlStr];
                    [imgs addObject:imageData];
                }
            }
            YBImageBrowser *browser = [YBImageBrowser new];
            browser.dataSourceArray = imgs;
            browser.currentPage = indexPath.row;
            [browser show];
        }
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.dataArray[indexPath.section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo) {
        return 30;
    } else if ([dic[cellType] intValue] == CellTypeWithImage) {
        TTDetailImageModel *imageModel = dic[cellDatas][indexPath.row];
        return imageModel.imageHeight;
    }
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    if ([dic[cellType] intValue] == CellTypeWithContentInfoHeader) {
        CGFloat h = self.descSectionH < 80 ? 80 : self.descSectionH;
        return 55 + h;
    }
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo || [dic[cellType] intValue] == CellTypeWithTitleHeader) {
        return [self createOneTwoSectionView:section];
    } else if ([dic[cellType] intValue] == CellTypeWithContentInfoHeader) {
        return [self createThreeSectionView:section];
    } else if ([dic[cellType] intValue] == CellTypeWithImage) {
        TTImageTitleView *titleView = [TTImageTitleView createViewWithLeftImageName:@"title_left_image" title:@"详情信息" rightImageName:@"title_right_image"];
        titleView.backgroundColor = UIColor.whiteColor;
        titleView.frame = CGRectMake(0, 0, kScreenWidth, 40);
        return titleView;
    } else {
        return [UIView new];
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo) {
        return [dic[cellDatas] count] == 0 ? 130 : 10;
    }
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    NSDictionary *dic = self.dataArray[section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo && [dic[cellDatas] count] == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 120)];
        view.backgroundColor = UIColor.whiteColor;

        TTUpDownButton *btn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"暂无出价记录!" forState:UIControlStateNormal];
        [btn setTitleColor:UIColorHex(999999) forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:13];
        [btn setImage:[UIImage imageNamed:@"no_price_record"] forState:UIControlStateNormal];
        [view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view);
            make.centerY.equalTo(view).mas_offset(-15);
            make.width.mas_equalTo(160);
            make.height.mas_equalTo(90);
        }];
        
        UIView *grayView = [[UIView alloc] init];
        grayView.backgroundColor = UIColorHex(#F2F2F2);
        [view addSubview:grayView];
        [grayView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(view);
            make.height.mas_equalTo(10);
        }];
        
        return view;
    }

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    return view;


}

- (UIView *)createOneTwoSectionView:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    view.backgroundColor = UIColor.whiteColor;
    UIImage *image = [UIImage imageWithColor:kMainColor rect:CGRectMake(0, 0, 3, 15) cornerRadius:1.5];
    UIButton *leftBtn = [UIButton createButtonWithTitle:@"" titleColor:UIColorHex(0x333333) titleFont:[UIFont boldSystemFontOfSize:15] backgroundColor:nil target:nil action:nil];
    [leftBtn setImage:image forState:UIControlStateNormal];
    leftBtn.frame = CGRectMake(15, 0, 30, view.height);
    [view addSubview:leftBtn];

    UILabel *subTitleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(0x666666) fontSize:13];
    [view addSubview:subTitleLabel];

    NSDictionary *dic = self.dataArray[section];
    if ([dic[cellType] intValue] == CellTypeWithPriceInfo) {
        [leftBtn setTitle:@" 我的出价记录" forState:UIControlStateNormal];
        subTitleLabel.text = @"";
    } else {
        if (self.detailModel.SalesModel == 2) {
            [leftBtn setTitle:@" 竞拍流程" forState:UIControlStateNormal];
            subTitleLabel.text = @"出价竞拍 > 竞拍结束 > 价高者得 > 交易完成";
        } else if (self.detailModel.SalesModel == 1) {
            [leftBtn setTitle:@" 询价流程" forState:UIControlStateNormal];
            subTitleLabel.text = @"联系业务 > 微信沟通 > 交易完成";
        } else {
            [leftBtn setTitle:@" 购买流程" forState:UIControlStateNormal];
            subTitleLabel.text = @"在线支付 > 商家发货 > 交易完成";
        }
    }
    
    [leftBtn sizeToFit];
    leftBtn.top = (view.height - leftBtn.height) * 0.5;
    [subTitleLabel sizeToFit];
    subTitleLabel.frame = CGRectMake(leftBtn.right + 12, 0, subTitleLabel.width, view.height);

    
    return view;
}

- (UIView *)createThreeSectionView:(NSInteger)section
{
    CGFloat h = self.descSectionH < 80 ? 80 : self.descSectionH;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 45 + h)];
    view.backgroundColor = UIColor.whiteColor;
    
    TTImageTitleView *titleView = [TTImageTitleView createViewWithLeftImageName:@"title_left_image" title:@"货品描述" rightImageName:@"title_right_image"];
    [view addSubview:titleView];
    [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(view);
        make.height.mas_equalTo(40);
    }];
    
    UILabel *contentLabel = [UILabel labelWithTitle:self.detailModel.Describe color:UIColorHex(666666) fontSize:13];
    contentLabel.numberOfLines = 0;
    [view addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView.mas_bottom).mas_offset(5);
        make.left.mas_equalTo(15);
        make.right.equalTo(view).mas_offset(-15);
        make.height.mas_equalTo(self.descSectionH);
    }];

    return view;

}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat alpha = scrollView.contentOffset.y / 100; //self.view.width;
    alpha = alpha >= 1 ? 1 : alpha;
    self.navigationController.navigationBar.alpha = alpha;
    self.navBarView.alpha = 1 - alpha;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    //如果向下偏移了，再偏移回去，否则顶部状态栏会出现空白
    if (offsetY < -kStatusBarHeight) {
        [self.tableView setContentOffset:CGPointMake(0, -kStatusBarHeight) animated:YES];
    }
}

// 点击状态栏回到了顶部
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    //如果向下偏移了，再偏移回去，否则顶部状态栏会出现空白
    if (offsetY < -kStatusBarHeight) {
        [self.tableView setContentOffset:CGPointMake(0, -kStatusBarHeight) animated:YES];
    }
}

/// 确定出价
- (void)confirmPrice:(NSString *)price
{
    NSDictionary *dic = @{
        @"param.realName" : [TTLoginSingle sharedSingle].loginModel.RealName,
        @"param.informationGoodsId" : self.goodsID,
        @"param.money" : price
    };
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:offerGoodsPrice_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        [MBProgressHUD showSuccess:@"出价成功"];
        [self requestDetailDatas];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
    } showLoadingInView:self.view];
    
}

///分享
- (void)shareAction
{
    self.shareView.image = self.shareImage;
}

/// 底部按钮点击了
- (void)bottomBtnClick
{
    if (self.detailModel.SalesModel == 1) {
        // 询价
        self.askPriceView.transform = CGAffineTransformIdentity;
        TYAlertController *alertVC = [TYAlertController alertControllerWithAlertView:self.askPriceView];
        alertVC.backgoundTapDismissEnable = YES;
        [self presentViewController:alertVC animated:YES completion:nil];
        
    } else if (self.detailModel.SalesModel == 2) {
        // 竞价
        if (self.status == isStart) {
            TTRecyclingDetailPriceListModel *priceModel = self.detailModel.Offer_Price_List.firstObject;
            [self.priceChangeView setupWithCurrentPrice:priceModel.Price.intValue startPrice:self.detailModel.Price.intValue priceRate:self.detailModel.Range.intValue];
            [self.priceChangeView showInWindowWithBackgoundTapDismissEnable:YES];
        }
    } else if (self.detailModel.SalesModel == 3) {
        // 一口价
        TTRecyclingCommitOrderViewController *orderVC = [[TTRecyclingCommitOrderViewController alloc] init];
        orderVC.model = self.detailModel;
        [self.navigationController pushViewController:orderVC animated:YES];
    }
}

- (TTRecyclingDetailHeaderView *)headerView
{
    if (!_headerView) {
        _headerView = [TTRecyclingDetailHeaderView headerViewWithDetailModel:nil];
        ///观察屏幕是否可旋转
        [_headerView.subject subscribeNext:^(id  _Nullable x) {
            AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            delegate.allowRotate = [x boolValue];
        }];
        // 只针对竞价的情况
        @weakify(self)
        _headerView.goodsStatusBlock = ^(GoodsStatus status) {
            @strongify(self)
            self.status = status;
            NSString *title; UIColor *color; BOOL canClick = NO;
            if (self.detailModel.SalesState == 3) {
                title = @"已下架";
                color = RGB(224, 224, 224);
                canClick = NO;
            } else if (self.detailModel.SalesState == 2) {
                title = @"已出售";
                color = RGB(224, 224, 224);
                canClick = NO;
            } else {
                // 正常
                if (status == upcoming) {
                    title = @"即将开始";
                    color = RGB(242, 158, 156);
                    canClick = NO;
                } else if (status == isStart) {
                    title = @"立即出价";
                    color = RGB(210, 79, 75);
                    canClick = YES;
                } else if (status == isEnd) {
                    title = @"已结束";
                    color = RGB(242, 158, 156);
                    canClick = NO;
                }
                if ([[TTLoginSingle sharedSingle].loginModel.RelationId isEqualToString:self.detailModel.RelationId]) {
                    title = @"自己的货品不能出价";
                    color = RGB(224, 224, 224);
                    canClick = NO;
                }
                
            }
            [self.bottomBtn setTitle:title forState:UIControlStateNormal];
            self.bottomBtn.backgroundColor = color;
            self.bottomBtn.enabled = canClick;
        };
        
        _headerView.shareBlock = ^{
            @strongify(self)
            [self shareAction];
        };
    }
    return _headerView;
}


- (UIView *)navBarView
{
    if (!_navBarView) {
        _navBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, StatusBarAndNavBar_Height)];
        [self.view insertSubview:_navBarView aboveSubview:self.tableView];

        UIButton *backBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"detail_nav_back"] target:self action:@selector(backAction)];
        backBtn.imageView.contentMode = UIViewContentModeCenter;
        backBtn.frame = CGRectMake(5, kStatusBarHeight, 44, 44);
        [_navBarView addSubview:backBtn];

        UIButton *shareBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"detailshare"] target:self action:@selector(shareAction)];
        shareBtn.imageView.contentMode = UIViewContentModeCenter;
        shareBtn.frame = CGRectMake(self.view.width - 44, kStatusBarHeight, 44, 44);
        [_navBarView addSubview:shareBtn];


    }
    return _navBarView;
}

- (UIButton *)bottomBtn
{
    if (!_bottomBtn) {
        _bottomBtn = [UIButton createButtonWithTitle:@"" titleColor:UIColor.whiteColor titleFont:[UIFont systemFontOfSize:15] backgroundColor:nil target:self action:@selector(bottomBtnClick)];
        if (Tabbar_Bottom_height > 0) {
            _bottomBtn.contentEdgeInsets = UIEdgeInsetsMake(-20, 0, 0, 0);
        }
        [self.view addSubview:_bottomBtn];
        [_bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
            make.top.equalTo(self.tableView.mas_bottom);
            make.left.right.equalTo(self.view);
        }];
    }
    return _bottomBtn;
}

- (TTRecyclingAskPriceView *)askPriceView
{
    if (!_askPriceView) {
        _askPriceView = [TTRecyclingAskPriceView createViewFromNib];
        _askPriceView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 50, 280);
        @weakify(self)
        _askPriceView.shareBlock = ^{
            @strongify(self)
            [self shareAction];
        };
    }
    return _askPriceView;
}

- (TTPriceChangeView *)priceChangeView
{
    if (!_priceChangeView) {
        _priceChangeView = [TTPriceChangeView createViewFromNib];
        _priceChangeView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 40, 235);
        
        @weakify(self)
        _priceChangeView.confirmPrice = ^(NSString * _Nonnull price) {
            @strongify(self)
            [self confirmPrice:price];
        };
        
    }
    _priceChangeView.transform = CGAffineTransformIdentity;
    return _priceChangeView;
}

- (TTRecyclingShareView *)shareView
{
    if (!_shareView) {
        _shareView = [[TTRecyclingShareView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        [[UIApplication sharedApplication].keyWindow addSubview:_shareView];
    }
    return _shareView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColorHex(#F2F2F2);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.emptyImageName = @"tao_nodata";
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[TTShoutOutPriceViewCell class] forCellReuseIdentifier:priceInfoCellID];
        [_tableView registerClass:[TTDetailImageViewCell class] forCellReuseIdentifier:imagesCellID];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];

        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop).mas_offset(-kStatusBarHeight);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideTop).mas_offset(-50);
        }];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
